class Solution:
    def maxRectangle(self, words):
        arcs=[]
        tempset={}
        result=[]
        area=0
        strs={}
        for i in range(len(words)):
            for j in range(i,len(words)):
                if words[i][0]==words[j][0]:
                    arcs.append((words[i],words[j])) 
            try:
                tempset[len(words[i])].append(words[i])
            except Exception:
                tempset[len(words[i])]=[words[i]]
        for (k,v) in tempset.items():
            strs[k]=" "+" ".join(v)
        def check(temp,width):
            nonlocal strs
            for i in range(len(temp[0])):
                cand="".join(j[i] for j in temp)
                if " "+cand not in strs[width]:
                    return False
            return True
        flag=True
        for (h,w) in arcs:
            if len(h)*len(w)>area:
                tempresult=[h]
                flag=True
                for i in range(1,len(w)):
                    for j in tempset[len(h)]:
                        if j[0]==w[i]:
                            tempresult.append(j)
                            if not check(tempresult,len(w)):
                                tempresult.pop()
                                continue
                    if len(tempresult)==i:
                        flag=False
                        break
                if flag:
                    area=len(h)*len(w)
                    result=tempresult
        return result
            
s=Solution()
print(s.maxRectangle(["aa"]))
